#include "pin_area.h"
#include "main_window.h"

PinArea::PinArea(QWidget* parent, int x, int y, int width, int height, int id, int serial) :
	Area(parent, x, y, width, height, id, serial)
{
    _isResizable = false;
    _image.load(":/icons/pin.png");
    setGeometry(this->x(), this->y(), _image.width()+18, _image.height()+6 );
}

void PinArea::setData(const QString &data)
{
    _filename = data;
}

void PinArea::paintEvent(QPaintEvent *)
{
    QPainter painter;
    painter.begin(this);
        painter.drawImage(QPoint(15,3), _image);
    painter.end();
}

QByteArray PinArea::getData()
{
    //QDesktopServices::openUrl(QUrl(QUrl::fromLocalFile(_filename)));
    QFile datafile(_filename);
    datafile.open(QIODevice::ReadOnly);

    QByteArray data = datafile.readAll();
    datafile.close();
    return data;
}

QByteArray PinArea::contains() const
{
    QString data = _filename;
    return ("<object type=pin x="+
            QByteArray::number(this->x())+" y="+
            QByteArray::number(this->y())+" w="+
            QByteArray::number(this->width())+" h="+
            QByteArray::number(this->height())+" r="+
            QByteArray::number(_color.red())+" g="+
            QByteArray::number(_color.green())+" b="+
            QByteArray::number(_color.blue())+" serial="+
            QByteArray::number(_serial)+">"+data.toUtf8()+"</object>\n");
}

bool PinArea::isEmpty() const
{
    return _filename.isEmpty();

}

